import { action, observable } from 'mobx';
import {
  listUserRole,
  saveUser,
  deleteUser,
  pinyin, listUserByOrgId, listUserByDeptId, listUserByName, listUserType,
} from '../service';
import loading from '../../../components/Loading';

class UserStore {

  // 用户列表参数
  @observable userListData = {
    dataSource: [],
    total: 0,
    loading: false,
    pageSize: 10,
    pageNo: 1,
  };

  // 用户角色参数
  @observable userDrawerData = {
    loading: false,
    visible: false,
    editData: {},
    rolesData: [],
  };


  @observable userType = [];

  // 选中部门节点
  currentDept = {};


  @action
  async deptOnload(node) {
    this.currentDept = node;
    this.listUser();
    this.userType = await listUserType();
  }

  @action
  async deptSelect(node) {
    this.currentDept = node;
    this.listUser();
  }

  async listUser(userName) {
    let userList;
    if (userName != null) {
      userList = await loading(listUserByName, this.userListData)(this.userListData.pageNo, this.userListData.pageSize, this.currentDept.orgId, userName);
    } else if (this.currentDept.deptId != null) {
      userList = await loading(listUserByDeptId, this.userListData)(this.userListData.pageNo, this.userListData.pageSize, this.currentDept.deptId);
    } else {
      userList = await loading(listUserByOrgId, this.userListData)(this.userListData.pageNo, this.userListData.pageSize, this.currentDept.orgId);
    }
    this.userListData = {
      ...this.userListData,
      dataSource: userList.rows,
      pageNo: userList.pageNo,
      pageSize: userList.pageSize,
      total: userList.total,
    };
  }

  @action
  async handlePaginationChange(pageNo, pageSize, searchName) {
    this.userListData = { ...this.userListData, pageNo: pageNo, pageSize: pageSize };
    this.listUser(searchName);
  }

  /**
   * 根据角色名称模糊查询角色列表
   * @param name 角色名称
   */
  @action
  async searchUser(name) {
    this.listUser(name);
  };

  /**
   * 打开用户编辑窗口
   * @param user 用户
   */
  async showUserDrawer(user) {
    if (user == null) {
      this.userDrawerData = {
        editData: { orgId: this.currentDept.orgId, deptId: this.currentDept.deptId },
        roles: [],
        visible: true,
      };
    } else {
      this.userDrawerData = { editData: user, roles: await listUserRole(user.uid), visible: true };
    }
  }


  async handlePinyin(name) {
    let editData = { ...this.userDrawerData.editData, pinyin: await pinyin(name) };
    this.userDrawerData = { ...this.userDrawerData, editData: editData };
  }

  /**
   * 关闭抽屉
   */
  @action
  closeUserDrawer() {
    this.userDrawerData = { ...this.userDrawerData, visible: false };
  }

  @action
  addUserRole(roles) {
    this.userDrawerData.roles = this.userDrawerData.roles.concat(roles);
  }

  @action
  deleteUserRole(item) {
    this.userDrawerData.roles.remove(item);
    this.userDrawerData = { ...this.userDrawerData };
  }

  /**
   * 保存用户数据，并关闭抽屉
   * @returns {Promise<void>}
   */
  @action
  async saveUser(user) {
    return await saveUser(user, this.userDrawerData.roles);
  }

  /**
   * 删除用户响应函数
   * @param record 角色
   */
  async deleteUser(record) {
    await deleteUser(record.uid);

    // 刷新用户列表
    this.userListData.dataSource.forEach((item, index, array) => {
      if (item.uid === record.uid) {
        array.splice(index, 1);
      }
    });
    this.userListData = { ...this.userListData };
  }


}

export default UserStore;
